package com.lulalei.dbviewer.commons.utils;

import com.lulalei.dbviewer.commons.bean.RowBean;
import com.lulalei.dbviewer.commons.bean.TableBean;
import com.lulalei.dbviewer.commons.enums.RowTypeEnums;
import com.lulalei.dbviewer.commons.exception.CustomizeException;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class JDBCResultHandlerUtils {

    /**
     * 将查询的表信息处理成规定的格式
     *
     * @param resultSet 查询出来的结果集
     * @return 需要的数据形式
     */
    public static List<TableBean> handleTables(ResultSet resultSet) {
        List<TableBean> list = new ArrayList<>();

        while (true) {
            TableBean tableBean = new TableBean();
            String tableName = null;
            String tableComment = null;
            //获取结果集中的值
            try {
                if (!resultSet.next()) break;
                tableName = resultSet.getString("tableName");
                tableComment = resultSet.getString("tableComment");
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //存进对象中
            tableBean.setTableJdbcName(tableName);
            tableBean.setName(NameCovertUtils.lineToHump(tableName));
            tableBean.setComment(tableComment);

            list.add(tableBean);
        }
        return list;
    }

    /**
     * 将查询的表信息处理成规定的格式
     *
     * @param resultSet 查询出来的结果集
     * @return 需要的数据形式
     */
    public static Integer handleCount(ResultSet resultSet) throws CustomizeException {

        while (true) {
            //获取结果集中的值
            try {
                if (!resultSet.next()) break;
                return resultSet.getInt("total");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        throw new CustomizeException("错误的结果集");
    }

    /**
     * 将查询的字段信息处理成规定的格式
     *
     * @param resultSet 查询出来的结果集
     * @return 需要的数据形式
     */
    public static List<RowBean> handleColumns(ResultSet resultSet) throws CustomizeException {
        List<RowBean> list = new ArrayList<>();

        while (true) {
            RowBean rowBean = new RowBean();
            String columnName = null;
            String columnType = null;
            String columnComment = null;
            String columnKey = null;
            //获取结果集中的值
            try {
                if (!resultSet.next()) break;
                columnName = resultSet.getString("columnName");
                columnType = resultSet.getString("columnType");
                columnComment = resultSet.getString("columnComment");
                columnKey = resultSet.getString("columnKey");
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //存进对象中
            rowBean.setJdbcName(columnName);
            rowBean.setName(NameCovertUtils.lineToHump(columnName));
            rowBean.setComment(columnComment);
            rowBean.setType(RowTypeEnums.getInstance(columnType).getJavaType());
            rowBean.setPrimaryKey("PRI".equals(columnKey));

            list.add(rowBean);
        }

        return list;
    }
}
